#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

// 定义链表节点结构
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (!head || k <= 1) return head;

        ListNode dummy(0);
        dummy.next = head;
        ListNode* prevgroupend = &dummy;

        while (true) {
            // 检查是否有足够的节点
            ListNode* cur = prevgroupend;
            for (int i = 0; i < k; ++i) {
                cur = cur->next;
                if (!cur) return dummy.next; // 剩余节点不足 k 个
            }

            // 翻转当前组
            ListNode* groupStart = prevgroupend->next;
            ListNode* nextGroupStart = cur->next;
            ListNode* prev = nullptr;
            ListNode* current = groupStart;

            while (current != nextGroupStart) {
                ListNode* next = current->next;
                current->next = prev;
                prev = current;
                current = next;
            }

            // 连接翻转后的链表
            prevgroupend->next = prev;
            groupStart->next = nextGroupStart;
            prevgroupend = groupStart;
        }
    }
};

// 从输入构造链表
ListNode* buildList(const vector<int>& values) {
    ListNode dummy(0);
    ListNode* tail = &dummy;
    for (int val : values) {
        tail->next = new ListNode(val);
        tail = tail->next;
    }
    return dummy.next;
}

// 输出链表
void printList(ListNode* head) {
    while (head) {
        cout << head->val;
        if (head->next) cout << " ";
        head = head->next;
    }
    cout << endl;
}

int main() {
    // 读取链表节点值
    string line;
    getline(cin, line);
    istringstream iss(line);
    vector<int> values;
    int val;
    while (iss >> val) {
        values.push_back(val);
    }

    // 读取 k
    int k;
    cin >> k;

    // 构造链表
    ListNode* head = buildList(values);

    // 处理链表
    Solution solution;
    ListNode* result = solution.reverseKGroup(head, k);

    // 输出结果链表
    printList(result);

    return 0;
}
